package jp.seraph.robocup.soccer.research.ikinematics;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import jp.seraph.jsade.math.Point3D;

import org.apache.commons.math.geometry.Vector3D;

public class TestCanvas extends Canvas {
    public TestCanvas(LineModel aModel, int aWidth, int aHeight) {
        mModel = aModel;
        this.setSize(aWidth, aHeight);

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_DOWN)
                    mModel.setEndPoint(mModel.getEndPoint().add(Vector3D.plusJ));
                else if (e.getKeyCode() == KeyEvent.VK_UP)
                    mModel.setEndPoint(mModel.getEndPoint().add(Vector3D.minusJ));
                else if (e.getKeyCode() == KeyEvent.VK_LEFT)
                    mModel.setEndPoint(mModel.getEndPoint().add(Vector3D.minusI));
                else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                    mModel.setEndPoint(mModel.getEndPoint().add(Vector3D.plusI));
                repaint();
            }
        });
    }

    private LineModel mModel;

    /**
     *
     * @see java.awt.Canvas#paint(java.awt.Graphics)
     */
    
    public void paint(Graphics aGraphics) {
        this.paint(mModel, aGraphics);
    }

    private void paint(LineModel aModel, Graphics aGraphics) {
        if (!aModel.isRoot())
            paint(aModel.getParent(), aGraphics);

        int[] tCenter = this.getCenter();
        int tEatio = 3;
        int tWidth = 3;

        int[] tXPoints = new int[5];
        int[] tYPoints = new int[5];
        Point3D tStartPoint = aModel.getStartPoint();
        Point3D tEndPoint = aModel.getEndPoint();
        double tRadAngle = Math.atan2(tEndPoint.getY() - tStartPoint.getY(), tEndPoint.getX() - tStartPoint.getX());

        double tXSub = tWidth * Math.sin(tRadAngle);
        double tYSub = tWidth * Math.cos(tRadAngle);
        tXPoints[0] = (int)(tStartPoint.getX() * tEatio - tXSub) + tCenter[0];
        tXPoints[1] = (int)(tStartPoint.getX() * tEatio + tXSub) + tCenter[0];
        tXPoints[2] = (int)(tEndPoint.getX() * tEatio + tXSub) + tCenter[0];
        tXPoints[3] = (int)(tEndPoint.getX() * tEatio - tXSub) + tCenter[0];
        tXPoints[4] = tXPoints[0];

        tYPoints[0] = (int)(tStartPoint.getY() * tEatio + tYSub) + tCenter[1];
        tYPoints[1] = (int)(tStartPoint.getY() * tEatio - tYSub) + tCenter[1];
        tYPoints[2] = (int)(tEndPoint.getY() * tEatio - tYSub) + tCenter[1];
        tYPoints[3] = (int)(tEndPoint.getY() * tEatio + tYSub) + tCenter[1];
        tYPoints[4] = tYPoints[0];


        aGraphics.fillPolygon(tXPoints, tYPoints, 5);
        //aGraphics.drawLine((int) aModel.getStartPoint().getX() * tEatio + tCenter[0], (int) aModel.getStartPoint().getY() * tEatio + tCenter[1], (int) aModel.getEndPoint().getX() * tEatio + tCenter[0], (int) aModel.getEndPoint().getY() * tEatio + tCenter[1]);

    }

    private int[] getCenter() {
        int[] tResult = new int[2];
        tResult[0] = this.getWidth() / 2;
        tResult[1] = this.getHeight() / 2;

        return tResult;
    }
}
